/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class Obj {
    public int num;
    
    Obj (int num) {
        this.num = num;
    }
}

class Allocate {
    public static void AllocateObj() {
        int count = 100000;
        Obj[] data = {new Obj(1), new Obj(1), new Obj(1), new Obj(1), new Obj(1)};// new Array();
        int[] resources = {12, 43, 56, 76, 89, 54, 45, 32, 35, 47, 46, 44, 21, 37, 84};
        double start = System.nanoTime();
        int resourcesLength = resources.length - 1;
        for (int i = 0; i < count; i++) {
            if ((resources[i & resourcesLength] & 1) == 0) {
                data[i & 4] = new Obj(i);
            } else {
                data[i & 4] = new Obj(i - 5);
            }
        }

        double end = System.nanoTime();
        int res = 0;
        for (int i = 0; i < data.length; i++) {
            res += data[i].num;
        }
        System.out.println("" + res);
        double time = (end - start) / 1000000;
        System.out.println("Allocate Obj:\t"+time+"\tms");
    }
}

class Benchmark {
    public static void main(String[] args) {
        Allocate.AllocateObj();
    }
}

